 /*本文件收集封装方法*/
/*
**	addCookie 		添加一个cookie
**	params 	
** 	name 	[String]
** 	value 	[String]
** 	iDay 	[Number]
*/
export const addCookie = function (name,value,iDay) {
	if(iDay){
		//设置过期时间
		var oDate = new Date();
		oDate.setDate(oDate.getDate()+iDay);
		document.cookie = name+'='+value+'; PATH=/; EXPIRES='+oDate.toGMTString();
	}else{
		//默认不设置过期时间
		document.cookie = name+'='+value+'; PATH=/';
	}
}
 
/*
** 	getCookie 		获取一个cookie
** 	params
** 	name 		[String]
*/
export const getCookie = function (name) {
	//name=李四; age=18; sex=男; weight=50
	var arr = document.cookie.split('; ');
	for(var i=0;i<arr.length;i++){
		var arr2 = arr[i].split('=');
		if(arr2[0]==name){
			return arr2[1];
		}
	}
}
/*
** 	removeCookie 		删除一个cookie
** 	params
** 	name 		[String]
*/
export const removeCookie = function (name) {
	addCookie(name,1,-1);
}
/**
 * 清除所有cookies
*/
export const clearAllCookie = function () {
	var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
	if(keys) {
		for(var i = keys.length; i--;)
			document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString()
	}
}

//localStorage长时间保存，sessionStorage会话结束前
/**
 * 存储localStorage
 */
export const setStore = (name, content) => {
    if (!name) return;
    if (typeof content !== 'string') {
        content = JSON.stringify(content);
    }
    window.localStorage.setItem(name, content);
}

/**
 * 获取localStorage
 */
export const getStore = name => {
    if (!name) return;
    return window.localStorage.getItem(name);
}
/*
*保存sessionStorage, setSession('name',JSON.stringify(state.shopcar))
*/
export const setSession = (name, value) => {
	if (!name) return;
	return window.sessionStorage.setItem(name, value)
}
/**
 * 读取sessionStorage getSession('name',JSON.parse(state.shopcar)) ,JSON.parse(getSession('name'))
*/
export const getSession = (name) => {
	if (!name) return;
	return window.sessionStorage.getItem(name)
}
/**
 * formDate
*/


/**
 *判断是不是有没有小数位 
 */
export const isDot = (num) => {
	var result = (num.toString()).indexOf(".");
	if(result != -1) {
			alert("含有小数内点容");
	} else {
			alert("不含小数点");
	}
}
/**
 * js处理数字保留2位小数,强制保留2位小数不够补上.00
 * 2 → 2.00  2.3  → 2.30
*/

export const changeTwoDecimal_f = (x) => {  
　　var f_x = parseFloat(x);  
　　if (isNaN(f_x))  
　　{  
　　　　return 0;  
　　}  
　　var f_x = Math.round(x*100)/100;  
　　var s_x = f_x.toString();  
　　var pos_decimal = s_x.indexOf('.');  
　　if (pos_decimal < 0)  
　　{  
　　　　pos_decimal = s_x.length;  
　　s_x += '.';  
　　}  
　　while (s_x.length <= pos_decimal + 2)  
　　{  
　　　　s_x += '0';  
　　}  
　　return s_x;  
}